package com.yiwyn.Creational.factory;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

public class FactoryMethodDemo {

    public static void main(String[] args) {

        /*画正方形*/
        IShapeFactory shapeFactory = new SquareFactory();
        Shape shape = shapeFactory.createShape();
        shape.draw();

        /*随机大小的三角形*/
        shapeFactory = new TriangleFactory();
        shape = shapeFactory.createRandomSizeShape();
        shape.draw();
    }

    /**
     * 图形工厂类
     */
    interface IShapeFactory {
        Shape createShape();

        Shape createRandomSizeShape();
    }

    static class SquareFactory implements IShapeFactory {

        @Override
        public Shape createShape() {
            return new Square("正方形");
        }

        @Override
        public Shape createRandomSizeShape() {
            return new Square("随机大小的正方形");
        }
    }

    static class TriangleFactory implements IShapeFactory {

        @Override
        public Shape createShape() {
            return new Triangle("三角形");
        }

        @Override
        public Shape createRandomSizeShape() {
            return new Triangle("随机大小的三角形");
        }
    }


    /**
     * 基本图形
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class Shape {
        private String name;

        public void draw() {
            System.out.println(name);
        }
    }

    /**
     * 正方形
     */
    @Data
    @EqualsAndHashCode(callSuper = true)
    static class Square extends Shape {
        public Square(String name) {
            super(name);
        }
    }

    /**
     * 三角形
     */
    @Data
    @EqualsAndHashCode(callSuper = true)
    static class Triangle extends Shape {
        public Triangle(String name) {
            super(name);
        }
    }


}
